#include "stdafx.h"
#include "PersistFile.h"
#include "LogFile.h"

#include <fstream>


PersistFile::PersistFile()
{
	IsOpen = false;
}


PersistFile::~PersistFile()
{
	if(IsOpen) {
		fclose(File);
		IsOpen = false;
	}
}

bool PersistFile::Open(const string & name) {
	if(!IsOpen) {
		File = fopen(name.c_str(), "a");
		if(File) {
			IsOpen = true;
			return true;
		}
	}
	return false;
}

void PersistFile::Close() {
	if( IsOpen ) {
		fclose(File);
	}
}

int PersistFile::Persist(const char *data) {
	return fprintf(File, "%s\n", data);
}

void PersistFile::LoadPersistFile(const string &name, ParseFunc pf, void *ctx) {
	ifstream  is;
	is.open(name);
	
	string line;
	while ( getline(is, line) ) {
		pf(line, ctx);
	}
}
